package com.example.gfsquadwebviewproject.fragment.setting;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.gfsquadwebviewproject.R;
import com.example.gfsquadwebviewproject.data.ConfigData;
import com.example.gfsquadwebviewproject.data.Constant;
import com.example.gfsquadwebviewproject.data.setting.MqttConfigData;
import com.example.gfsquadwebviewproject.presenter.service.mqtt.IMqttCallbcak;
import com.example.gfsquadwebviewproject.util.DeviceUtils;
import com.example.gfsquadwebviewproject.util.L;

public class MqttConfigFragment extends Fragment {
    String TAG = "MqttConfigFragment";

    Context context;
    MqttConfigData mqttConfigData;
    ConfigData configData;
    DeviceUtils deviceUtils;

    TextView et_ip;
    TextView et_port;
    TextView tv_mqtt_connect_status;
    TextView tv_mqtt_topic;
    TextView tv_mqtt_clientId;
    Button bt_save;

    IMqttCallbcak iMqttCallbcak;

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, final Intent intent) {
            try {
                if (intent.getAction().equals(Constant.ACTION_MQTT_CONNECT_STATUS)) {
                    try {
                        tv_mqtt_connect_status.setText(Constant.isMqttConnect+"");
                    } catch (Exception e) {
                        L.wf(TAG,"receiver error:"+e);
                    }
                }
            } catch (Exception e) {

            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = getContext();
        mqttConfigData = new MqttConfigData(context);
        configData = new ConfigData(context);
        deviceUtils = new DeviceUtils(context);

        // 注册广播监听
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.ACTION_MQTT_CONNECT_STATUS);
        context.registerReceiver(receiver, intentFilter);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.setting_mqtt_config_fragment,container,false);

        et_ip = view.findViewById(R.id.et_ip);
        et_port = view.findViewById(R.id.et_port);
        tv_mqtt_connect_status = view.findViewById(R.id.tv_mqtt_connect_status);
        tv_mqtt_clientId = view.findViewById(R.id.tv_mqtt_clientId);
        bt_save = view.findViewById(R.id.bt_save);
        tv_mqtt_topic = view.findViewById(R.id.tv_mqtt_topic);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getData();
        tv_mqtt_clientId.setText(deviceUtils.getMac());
        tv_mqtt_topic.setText(configData.getClassId()+"/#");
        tv_mqtt_connect_status.setText(Constant.isMqttConnect+"");
        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveData();
            }
        });
    }

    private void getData(){
        et_ip.setText(mqttConfigData.getMqtt_ip()+"");
        et_port.setText(mqttConfigData.getMqtt_port()+"");
    }

    private void saveData(){
        try {
            String ip = et_ip.getText().toString().trim();
            String portStr = et_port.getText().toString();

            if (ip.length() < 1) {
                et_ip.setError("请输入ip");
                return;
            }

            if (portStr.length() != 4) {
                et_port.setError("请输入4位数字的端口号");
                return;
            }
            Toast.makeText(context,"保存完成",Toast.LENGTH_SHORT).show();

            int port = Integer.valueOf(portStr);

            if (!ip.equals(mqttConfigData.getMqtt_ip()) || port!=mqttConfigData.getMqtt_port()){
                mqttConfigData.setMqtt_ip(ip);
                mqttConfigData.setMqtt_port(port);

                iMqttCallbcak.reconnectMqttService();
            }

        }
        catch (Exception e){
            L.wf(TAG,"saveData error:"+e);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        iMqttCallbcak = (IMqttCallbcak) activity;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        context.unregisterReceiver(receiver);
    }

}